package com.teddy.customWidgets;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Text;

import com.hp.hpl.jena.rdf.model.Property;
import com.hp.hpl.jena.vocabulary.RDFS;
import com.teddy.controller.RDFSchemaControl;
import com.teddy.view.BaseGUI;
import com.teddy.view.tree.TaxonomyTreeItem;

public class RDFSPropertiesGroup extends BasePropertiesGroup {

	private Composite literalComposite;
	private TaxonomyTreeItem tree;
	
	public RDFSPropertiesGroup(Composite parent, int style, TaxonomyTreeItem tree) {
		super(parent, style);

		this.tree = tree;
		setLayout(new GridLayout( 1, true ));
		setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, true) );
		setText( RDFSchemaControl.getResourceString( "rdfs.title"));
	}

	@Override
	public void createContent() {
		literalComposite = new Composite( this, SWT.NONE );
		GridLayout gridLayout = new GridLayout(2, true);
		gridLayout.horizontalSpacing = 20;
		literalComposite.setLayout( gridLayout );
		literalComposite.setLayoutData( new GridData( SWT.FILL, SWT.FILL, true, true ) );
		
		BaseGUI.setRdfsComment( createPropertiesSingleValue2( literalComposite, 
															  "rdfs.comment", 
															  tree.getLiteralProperty(RDFS.comment), 
															  false, RDFS.comment ));
		BaseGUI.setRdfsLabel( createPropertiesSingleValue2( literalComposite, 
															"rdfs.label", 
															tree.getLiteralProperty(RDFS.label), 
															false, RDFS.label ));
		BaseGUI.setRdfsDomain( createSingleValueLinkProperty( literalComposite, 
															  "rdfs.domain", 
															  tree.getLiteralProperty(RDFS.domain), RDFS.domain ));
		BaseGUI.setRdfsIsDefinedBy( createSingleValueLinkProperty( literalComposite, 
																   "rdfs.isDefinedBy", 
																   tree.getLiteralProperty(RDFS.isDefinedBy), RDFS.isDefinedBy ));
		BaseGUI.setRdfsMember( createSingleValueLinkProperty( literalComposite, 
															  "rdfs.member", 
															  tree.getLiteralProperty(RDFS.member), RDFS.member ));
		BaseGUI.setRdfsRange(createSingleValueLinkProperty( literalComposite, 
															"rdfs.range", 
															tree.getLiteralProperty(RDFS.range), RDFS.range ));
		BaseGUI.setRdfsSeeAlso( createSingleValueLinkProperty( literalComposite, 
															   "rdfs.seeAlso", 
															   tree.getLiteralProperty(RDFS.seeAlso),  RDFS.seeAlso  ));
		BaseGUI.setRdfsSubClassOf( createSingleValueLinkProperty( literalComposite, 
																  "rdfs.subClassOf",
																  tree.getLiteralProperty(RDFS.subClassOf), RDFS.subClassOf ));
		BaseGUI.setRdfsSubPropertyOf( createSingleValueLinkProperty( literalComposite, 
																	 "rdfs.subPropertyOf",
																	 tree.getLiteralProperty(RDFS.subPropertyOf), RDFS.subPropertyOf ));
	}
	
	protected Text createPropertiesSingleValue2( Composite parent, 
			String labelKey, 
			String value, 
			boolean isMultiLine, 
			Property p ) {
		final Property prop = p;
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		Text editBox;
		GridData gridData;
		if( isMultiLine ) {
			editBox = new Text( propertyComposite, SWT.WRAP | SWT.MULTI | SWT.NONE );
			gridData = new GridData(SWT.FILL, SWT.FILL, true, false);
			gridData.minimumHeight = 50;
			editBox.setLayoutData( gridData );
		} else {
			editBox = new Text( propertyComposite, SWT.NONE );
			editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		}
		editBox.setText(value);
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		editBox.addModifyListener(new ModifyListener() {
			
			@Override
			public void modifyText(ModifyEvent e) {
				Text text = (Text) e.widget;
				tree.setLiteralSingleProperty(prop, text.getText());//.getDcModel().setSimpleListeralValue(prop, text.getText());
			}
		});
		
		return editBox;
	}
	
	protected Link createSingleValueLinkProperty( Composite parent, 
			  String labelKey, 
			  String value, 
			  Property p) {
		Composite propertyComposite = new Composite( parent, SWT.NONE );
		propertyComposite.setLayout(new GridLayout(1, false));
		propertyComposite.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false ));
		
		Label label = new Label( propertyComposite, SWT.NONE );
		label.setText( RDFSchemaControl.getResourceString( labelKey + ".label") );
		label.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		
		Link editBox;
		editBox = new Link( propertyComposite, SWT.NONE );
		editBox.setLayoutData( new GridData(SWT.FILL, SWT.FILL, true, false));
		editBox.setText( "<A>" + value + "</A>");
		editBox.setToolTipText( RDFSchemaControl.getResourceString( labelKey + ".tooltip" ) );
		return editBox;
	}

}
